package com.fengxing.ams.web.common;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Repository;

import com.fengxing.ams.pub.manager.intf.IConfigManager;
import com.fengxing.ams.web.facade.mouble.IZytRedis;
import com.fengxing.ams.web.facade.mouble.college.ICollegeDAO;
import com.fengxing.ams.web.facade.mouble.company.ICompanyImportDAO;
import com.fengxing.ams.web.facade.mouble.enrollment.IMajorEnrollmentDAO;
import com.fengxing.ams.web.facade.mouble.enrollment.IMajorEnrollmentRedis;
import com.fengxing.ams.web.facade.mouble.plan.IPlanDAO;
import com.fengxing.ams.web.facade.mouble.plan.IPlanRedis;
import com.fengxing.ams.web.facade.mouble.plan.PlanCacheVO;
import com.fengxing.ams.web.facade.mouble.plan.PlanVO;
import com.fengxing.ams.web.facade.mouble.user.IUserDAO;
import com.fengxing.ams.web.facade.mouble.user.UserVO;
@Repository
public class InitManager implements ApplicationListener<ContextRefreshedEvent> {

	private static final Log logger = LogFactory
			.getLog(InitManager.class);
	
	@Autowired
	IMajorEnrollmentRedis majorEnrollmentRedis;
	
	@Autowired
	IMajorEnrollmentDAO majorEnrollmentDAO;
	
	@Autowired
	IConfigManager configManager;
	
	@Autowired
	ICollegeDAO collegeDAO;
	
	@Autowired
	IZytRedis zytRedis;
	
	@Autowired
	IContextManager contextManager;
	
	@Autowired
	IPlanDAO planDAO;
	
	@Autowired
	IPlanRedis planRedis;
	
	@Autowired
	IUserDAO userDAO;
	
	@Autowired
	ICompanyImportDAO comanyImportDAO;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		 if(event.getApplicationContext().getParent() == null){//root application context 没有parent，他就是老大.
			 
			 if (logger.isDebugEnabled()) {
				 logger.debug("start init major enroll cache");
			 }
			 
			 List<String> studentIds = this.userDAO.findAllStudentId();
			 List<String> ids = this.comanyImportDAO.findAllIds();
			 this.configManager.addAllStudentId(studentIds);
			 this.configManager.addAllStudentId(ids);
			 
			 List<UserVO> users = this.userDAO.findAllManagerUser();
			 UserVO userVO = this.userDAO.findSupperManagerUser();
			 
			 this.contextManager.setAllManagerUser(users);
			 this.contextManager.setSupperManagerUser(userVO);
			 
			 if (logger.isDebugEnabled()) {
				 logger.debug("end init major enroll cache");
			 }
        }
	}
	
	private PlanCacheVO	 convert(PlanVO vo) throws IllegalAccessException, InvocationTargetException {
		PlanCacheVO cache = new PlanCacheVO();
		BeanUtils.copyProperties(cache, vo);
		return cache;
	}

}
